package com.ruoyi.demo.controller;


import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.demo.domain.House;
import com.ruoyi.demo.domain.Vov;
import com.ruoyi.demo.service.HouseService;
import com.ruoyi.demo.util.EsUtil;
import jakarta.json.Json;
import org.apache.catalina.security.SecurityUtil;
import org.apache.commons.collections.ArrayStack;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;


@CrossOrigin
@RestController
@RequestMapping("house")
public class HouseController {


    @Autowired
    HouseService houseService;





    @PostMapping("add")
    public Map<String,Object> add(@RequestBody House house){
        HashMap<String, Object> map = new HashMap<>();
        boolean save = houseService.save(house);
        if (save){
            EsUtil.insertData("wei","id_"+System.currentTimeMillis(), JSON.toJSONString(house));
             map.put("code",200);
             return map;
         }
         map.put("code",201);
         return map;
    }






  /*  @RequestMapping("getHouse")
    public List<House> qwe(House house){

        QueryBuilder queryBuilder1=null;
        //mohu
        if (house.getName()!=null && house.getName()!=""){

            queryBuilder1 = QueryBuilders.matchQuery("name", house.getName());
        }
        if (house.getZhuang()!=null && house.getZhuang()!=""){

            queryBuilder1 = QueryBuilders.matchQuery("zhuang", house.getZhuang());
        }

        List<House> list = EsUtil.searchData("wei", new SearchSourceBuilder(), queryBuilder1, House.class);
        return list;
    }*/



/*    @RequestMapping("getHouse")
    public Page qwe(House house,Integer pageNum,Integer pageSize){


        QueryBuilder queryBuilder1=null;
        //mohu
        if (house.getName()!=null && house.getName()!=""){

            queryBuilder1 = QueryBuilders.matchQuery("name", house.getName());
        }
        if (house.getZhuang()!=null && house.getZhuang()!=""){

            queryBuilder1 = QueryBuilders.matchQuery("zhuang", house.getZhuang());
        }

        Page<? extends House> page = EsUtil.searchDataPage("wei", pageNum, pageSize, new SearchSourceBuilder(), queryBuilder1, house.getClass());
        return page;
    }*/






    @RequestMapping("getHouse")
    public Map<String,Object> qwe(House house,Integer pageNum,Integer pageSize) throws IOException {

        HashMap<String, Object> map = new HashMap<>();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        //条件查询  判断是否存在
        if (StringUtils.isNotEmpty(house.getName())){
            boolQuery.filter(QueryBuilders.matchQuery("name",house.getName()));
        }
        //条件查询  判断是否存在
        if (StringUtils.isNotEmpty(house.getZhuang())){
            boolQuery.filter(QueryBuilders.matchQuery("zhuang",house.getZhuang()));
        }
        //执行查询
        SearchResponse search = EsUtil.getSearchResponse("wei", pageNum, pageSize, new SearchSourceBuilder(), boolQuery);
        //解析结果
        ArrayList<Map<String,Object>> list = new ArrayList<>();

        SearchHit[] hits = search.getHits().getHits();

        for (SearchHit hit : hits) {
            System.err.println("@@@@@@@@@@@@@@@@@"+hit);
            //解析高亮的字段
            //获取高亮字段
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField name = highlightFields.get("name");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();//原来的结果
            //将原来的字段替换为高亮字段即可
            if (name!=null){
                Text[] fragments = name.fragments();
                String newTitle = "";
                for (Text text : fragments) {
                    newTitle +=text;
                }
                sourceAsMap.put("name",newTitle);//替换掉原来的内容
            }
            list.add(sourceAsMap);
        }
        map.put("list",list);
        return map;

    }





    @RequestMapping("file")
    public String file(MultipartFile file) throws IOException {
        if (null!=file && file.getSize()>0){
            String filename = file.getOriginalFilename();
            String s = UUID.randomUUID() + filename.substring(filename.lastIndexOf("."));
            File file1 = new File("D:\\pic", s);
            file.transferTo(file1);
            return "/pic/"+s;
        }
        return null;
    }





}
